﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using FluentData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class AdverseEventReasonManager : BaseManager
    {
        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="adverseEventReason"></param>
        /// <returns></returns>
        public static OperateResult Save(AdverseEventReason adverseEventReason)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();

                if (adverseEventReason.ID == 0)
                {
                    adverseEventReason.CreateUserID = loginUser.Personnel_id;
                    adverseEventReason.CreateTime = DateTime.Now;
                    int reasonID = AdverseEventReasonService.Add(adverseEventReason);
                    if (reasonID == 0)
                    {
                        resultInfo.Result = 0;
                    }
                }
                else
                {
                    AdverseEventReason oldAdverseEventReason = AdverseEventReasonService.GetQuerySingleByID(adverseEventReason.ID);
                    oldAdverseEventReason.Reason = adverseEventReason.Reason;
                    oldAdverseEventReason.ParentID = adverseEventReason.ParentID;
                    oldAdverseEventReason.Sort = adverseEventReason.Sort;
                    int rows = AdverseEventReasonService.Update(oldAdverseEventReason);
                    if (rows == 0)
                    {
                        resultInfo.Result = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult Delte(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                int count = AdverseEventReasonService.GetChildrenCount(id);
                if (count > 0)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "有子级数据不能删除";
                }
                if (resultInfo.Result == 1)
                {
                    int rows = AdverseEventReasonService.DelByID(id);
                    if (rows == 0)
                    {
                        resultInfo.Result = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
